<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <?php
    session_start();
    if (!isset($_SESSION['userid']))
    {
        echo"User not logged in";
        echo"</html>";
        exit();
    }
    if (isset($_POST['logout']))
    {
        include 'ClassLibs/statics.php';
        vLogout();
        header("Location: index.php");
    }
    if (isset($_POST['newpost']) && $_POST['choice'] === "text" && strpos($_SESSION['prev_posts'], $_POST['post']) === false)
    {
        include 'ClassLibs/statics.php';
        if (bAddTextPost($_SESSION['userid'], $_POST['post']))
        {
            $_SESSION['prev_posts'] .= $_POST['post'];
            echo"Posted successfully";
        }
        else
        {
            echo"Error storing post";
        }
    }
    else if (isset($_POST['newpost']) && $_POST['choice'] === "pdf" && strpos($_SESSION['prev_posts'], $_FILES['fileupload']['name']) === false)
    {
        include 'ClassLibs/statics.php';
        if (isset($_FILES['fileupload']) && $_FILES['fileupload']['error'] === 0)
        {
            if (bAddPdfPost($_SESSION['userid'], $_FILES['fileupload']['name'], $_FILES['fileupload']['type'], $_FILES['fileupload']['tmp_name']))
            {
                $_SESSION['prev_posts'] .= $_FILES['fileupload']['name'];
                echo "Posted successfully";
            }
            else
            {
                echo "Error storing post, make sure the file is a valid PDF file with a size under 16Mb";
            }
        }
    }
    else if ($_SESSION['prev_posts'] != "")
    {
        echo "Session data conflict: either post was just submitted or you used the back/refresh button";
    }
    ?>
    <head>       
        <link rel="stylesheet" type="text/css" href="index_style.css"/>
        <script type="text/javascript">
            function vShowHide()
            {
                if(document.getElementById("text").checked)
                {
                    document.getElementById("post").style.visibility="visible";
                }
                else
                {
                    document.getElementById("post").style.visibility = "hidden";
                }
                if(document.getElementById("pdf").checked)
                {
                    document.getElementById("file").style.visibility="visible";
                }
                else
                {
                    document.getElementById("file").style.visibility="hidden";
                }
                        
            }
        </script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>       
        <form method="POST" action="BlogMain.php">
            <input type="submit" name="blogmain" value="Back to blog main"/>
        </form>
        <form method="POST">
            <input type="submit" name="logout" value="Log out"/>
        </form>

        <form enctype="multipart/form-data" method="POST">
            <input type="radio" name="choice" value="text" id="text" onclick="vShowHide()"/><label for="text">Text</label><br/>
            <input type="radio" name="choice" value="pdf" id="pdf" onclick="vShowHide()"/><label for="pdf">PDF</label><br/>
            <input type="submit" name="newpost" value="Post"/><br/>
            <p><input type="file" name="fileupload" style="visibility: hidden" id="file"/></p>
            <p><textarea name="post" rows="50" cols="50" style="visibility: hidden" id="post"></textarea></p>
        </form>
    </body>
</html>


